package com.xiaofeng.wx.controller;

import com.xiaofeng.wx.hadnler.WxChatMsgFactory;
import com.xiaofeng.wx.hadnler.WxChatMsgHandler;
import com.xiaofeng.wx.utils.MessageUtil;
import com.xiaofeng.wx.utils.SHA1;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author: chc
 * @Date: 2024/02/26/15:06
 * @Description:
 */
@RestController
@Slf4j
public class CallBackController {

    private static final String token ="xiaofengasdsdasd";

    @Resource
    private WxChatMsgFactory wxChatMsgFactory;


    private static final String CACHE_KEY_SEPARATOR = ".";

    @RequestMapping("/test")
    public String test(){
        return "hello world";
    }

    /**
     * 回调消息检验
     * @param signature
     * @param timestamp
     * @param nonce
     * @param echostr
     * @return
     */
    @GetMapping("/callback")
    public String callBack(@RequestParam("signature") String signature,
                           @RequestParam("timestamp") String timestamp,
                           @RequestParam("nonce") String nonce,
                           @RequestParam("echostr") String echostr){
        log.info("get验签请求参数：signature:{},signature:{},nonce:{},nonce:{}",
                signature,timestamp,nonce,echostr);
        String shaStr = SHA1.getSHA1(token, timestamp, nonce, "");
        if(signature.equals(shaStr)){
            return echostr;
        }
        return "unknown";
    }

    @PostMapping(value = "callback", produces = "application/xml;charset=UTF-8")
    public String callback(
            @RequestBody String requestBody,
            @RequestParam("signature") String signature,
            @RequestParam("timestamp") String timestamp,
            @RequestParam("nonce") String nonce,
            @RequestParam(value = "msg_signature", required = false) String msgSignature) {
        log.info("接收到微信消息：requestBody：{}", requestBody);
        Map<String, String> messageMap = MessageUtil.parseXml(requestBody);
        String msgType = messageMap.get("MsgType");
        String event = messageMap.get("Event") == null ? "" : messageMap.get("Event");
        log.info("msgType:{},event:{}",msgType,event);
        StringBuilder sb = new StringBuilder();
        sb.append(msgType);
        if(!StringUtils.isEmpty(event)){
            sb.append(".");
            sb.append(event);
        }
        String msgTypeKey = sb.toString();
        WxChatMsgHandler wxChatMsgHandler = wxChatMsgFactory.getWxChatMsgHandler(msgTypeKey);
        String replyContent = wxChatMsgHandler.dealMsg(messageMap);
        log.info("replyContent:{}",replyContent);
        return replyContent;
    }

    // String content = "<xml>\n" +
    //         "  <ToUserName><![CDATA["+fromUserName+"]]></ToUserName>\n" +
    //         "  <FromUserName><![CDATA["+toUserName+"]]></FromUserName>\n" +
    //         "  <CreateTime>12345678</CreateTime>\n" +
    //         "  <MsgType><![CDATA[text]]></MsgType>\n" +
    //         "  <Content><![CDATA[你好，我叫晓风，欢迎来学习!]]></Content>\n" +
    //         "</xml>";
}
